home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / bsd-ftpd.000 / bsd-ftpd / bsd.ftpd / ftpcmd.y < prev    next >
Encoding:
Text File  |  1995-09-09  |  24.5 KB  |  1,279 lines

  1. /*    $NetBSD: ftpcmd.y,v 1.6 1995/06/03 22:46:45 mycroft Exp $    */
  2.  
  3. /*
  4.  * Copyright (c) 1985, 1988, 1993, 1994
  5.  *    The Regents of the University of California.  All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *    This product includes software developed by the University of
  18.  *    California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  *
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  *
  35.  *    @(#)ftpcmd.y    8.3 (Berkeley) 4/6/94
  36.  */
  37.  
  38. /*
  39.  * Grammar for FTP commands.
  40.  * See RFC 959.
  41.  */
  42.  
  43. %{
  44.  
  45. #ifndef lint
  46. #if 0
  47. static char sccsid[] = "@(#)ftpcmd.y    8.3 (Berkeley) 4/6/94";
  48. #else
  49. static char rcsid[] = "$NetBSD: ftpcmd.y,v 1.6 1995/06/03 22:46:45 mycroft Exp $";
  50. #endif
  51. #endif /* not lint */
  52.  
  53. #include "glob.h"
  54.  
  55. #include <sys/param.h>
  56. #include <sys/socket.h>
  57. #include <sys/stat.h>
  58.  
  59. #include <netinet/in.h>
  60. #include <arpa/ftp.h>
  61.  
  62. #include <ctype.h>
  63. #include <errno.h>
  64. #include <pwd.h>
  65. #include <setjmp.h>
  66. #include <signal.h>
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <string.h>
  70. #include <syslog.h>
  71. #include <time.h>
  72. #include <unistd.h>
  73.  
  74. #include "extern.h"
  75.  
  76. extern    struct sockaddr_in data_dest;
  77. extern    int logged_in;
  78. extern    struct passwd *pw;
  79. extern    int guest;
  80. extern    int logging;
  81. extern    int type;
  82. extern    int form;
  83. extern    int debug;
  84. extern    int timeout;
  85. extern    int maxtimeout;
  86. extern  int pdata;
  87. extern    char hostname[], remotehost[];
  88. extern    char proctitle[];
  89. extern    int usedefault;
  90. extern  int transflag;
  91. extern  char tmpline[];
  92.  
  93. off_t    restart_point;
  94.  
  95. static    int cmd_type;
  96. static    int cmd_form;
  97. static    int cmd_bytesz;
  98. char    cbuf[512];
  99. char    *fromname;
  100.  
  101. %}
  102.  
  103. %union {
  104.     int    i;
  105.     char   *s;
  106. }
  107.  
  108. %token
  109.     A    B    C    E    F    I
  110.     L    N    P    R    S    T
  111.  
  112.     SP    CRLF    COMMA
  113.  
  114.     USER    PASS    ACCT    REIN    QUIT    PORT
  115.     PASV    TYPE    STRU    MODE    RETR    STOR
  116.     APPE    MLFL    MAIL    MSND    MSOM    MSAM
  117.     MRSQ    MRCP    ALLO    REST    RNFR    RNTO
  118.     ABOR    DELE    CWD    LIST    NLST    SITE
  119.     STAT    HELP    NOOP    MKD    RMD    PWD
  120.     CDUP    STOU    SMNT    SYST    SIZE    MDTM
  121.  
  122.     UMASK    IDLE    CHMOD
  123.  
  124.     LEXERR
  125.  
  126. %token    <s> STRING
  127. %token    <i> NUMBER
  128.  
  129. %type    <i> check_login octal_number byte_size
  130. %type    <i> struct_code mode_code type_code form_code
  131. %type    <s> pathstring pathname password username
  132.  
  133. %start    cmd_list
  134.  
  135. %%
  136.  
  137. cmd_list
  138.     : /* empty */
  139.     | cmd_list cmd
  140.         {
  141.             fromname = (char *) 0;
  142.             restart_point = (off_t) 0;
  143.         }
  144.     | cmd_list rcmd
  145.     ;
  146.  
  147. cmd
  148.     : USER SP username CRLF
  149.         {
  150.             user($3);
  151.             free($3);
  152.         }
  153.     | PASS SP password CRLF
  154.         {
  155.             pass($3);
  156.             free($3);
  157.         }
  158.     | PORT SP host_port CRLF
  159.         {
  160.             usedefault = 0;
  161.             if (pdata >= 0) {
  162.                 (void) close(pdata);
  163.                 pdata = -1;
  164.             }
  165.             reply(200, "PORT command successful.");
  166.         }
  167.     | PASV CRLF
  168.         {
  169.             passive();
  170.         }
  171.     | TYPE SP type_code CRLF
  172.         {
  173.             switch (cmd_type) {
  174.  
  175.             case TYPE_A:
  176.                 if (cmd_form == FORM_N) {
  177.                     reply(200, "Type set to A.");
  178.                     type = cmd_type;
  179.                     form = cmd_form;
  180.                 } else
  181.                     reply(504, "Form must be N.");
  182.                 break;
  183.  
  184.             case TYPE_E:
  185.                 reply(504, "Type E not implemented.");
  186.                 break;
  187.  
  188.             case TYPE_I:
  189.                 reply(200, "Type set to I.");
  190.                 type = cmd_type;
  191.                 break;
  192.  
  193.             case TYPE_L:
  194. #if CHAR_BIT == 8
  195.                 if (cmd_bytesz == 8) {
  196.                     reply(200,
  197.                         "Type set to L (byte size 8).");
  198.                     type = cmd_type;
  199.                 } else
  200.                     reply(504, "Byte size must be 8.");
  201. #else /* CHAR_BIT == 8 */
  202.                 UNIMPLEMENTED for CHAR_BIT != 8
  203. #endif /* CHAR_BIT == 8 */
  204.             }
  205.         }
  206.     | STRU SP struct_code CRLF
  207.         {
  208.             switch ($3) {
  209.  
  210.             case STRU_F:
  211.                 reply(200, "STRU F ok.");
  212.                 break;
  213.  
  214.             default:
  215.                 reply(504, "Unimplemented STRU type.");
  216.             }
  217.         }
  218.     | MODE SP mode_code CRLF
  219.         {
  220.             switch ($3) {
  221.  
  222.             case MODE_S:
  223.                 reply(200, "MODE S ok.");
  224.                 break;
  225.  
  226.             default:
  227.                 reply(502, "Unimplemented MODE type.");
  228.             }
  229.         }
  230.     | ALLO SP NUMBER CRLF
  231.         {
  232.             reply(202, "ALLO command ignored.");
  233.         }
  234.     | ALLO SP NUMBER SP R SP NUMBER CRLF
  235.         {
  236.             reply(202, "ALLO command ignored.");
  237.         }
  238.     | RETR check_login SP pathname CRLF
  239.         {
  240.             if ($2 && $4 != NULL)
  241.                 retrieve((char *) 0, $4);
  242.             if ($4 != NULL)
  243.                 free($4);
  244.         }
  245.     | STOR check_login SP pathname CRLF
  246.         {
  247.             if ($2 && $4 != NULL)
  248.                 store($4, "w", 0);
  249.             if ($4 != NULL)
  250.                 free($4);
  251.         }
  252.     | APPE check_login SP pathname CRLF
  253.         {
  254.             if ($2 && $4 != NULL)
  255.                 store($4, "a", 0);
  256.             if ($4 != NULL)
  257.                 free($4);
  258.         }
  259.     | NLST check_login CRLF
  260.         {
  261.             if ($2)
  262.                 send_file_list(".");
  263.         }
  264.     | NLST check_login SP STRING CRLF
  265.         {
  266.             if ($2 && $4 != NULL)
  267.                 send_file_list($4);
  268.             if ($4 != NULL)
  269.                 free($4);
  270.         }
  271.     | LIST check_login CRLF
  272.         {
  273.             if ($2)
  274.                 retrieve("/bin/ls -lgA", "");
  275.         }
  276.     | LIST check_login SP pathname CRLF
  277.         {
  278.             if ($2 && $4 != NULL)
  279.                 retrieve("/bin/ls -lgA %s", $4);
  280.             if ($4 != NULL)
  281.                 free($4);
  282.         }
  283.     | STAT check_login SP pathname CRLF
  284.         {
  285.             if ($2 && $4 != NULL)
  286.                 statfilecmd($4);
  287.             if ($4 != NULL)
  288.                 free($4);
  289.         }
  290.     | STAT CRLF
  291.         {
  292.             statcmd();
  293.         }
  294.     | DELE check_login SP pathname CRLF
  295.         {
  296.             if ($2 && $4 != NULL)
  297.                 delete($4);
  298.             if ($4 != NULL)
  299.                 free($4);
  300.         }
  301.     | RNTO SP pathname CRLF
  302.         {
  303.             if (fromname) {
  304.                 renamecmd(fromname, $3);
  305.                 free(fromname);
  306.                 fromname = (char *) 0;
  307.             } else {
  308.                 reply(503, "Bad sequence of commands.");
  309.             }
  310.             free($3);
  311.         }
  312.     | ABOR CRLF
  313.         {
  314.             reply(225, "ABOR command successful.");
  315.         }
  316.     | CWD check_login CRLF
  317.         {
  318.             if ($2)
  319.                 cwd(pw->pw_dir);
  320.         }
  321.     | CWD check_login SP pathname CRLF
  322.         {
  323.             if ($2 && $4 != NULL)
  324.                 cwd($4);
  325.             if ($4 != NULL)
  326.                 free($4);
  327.         }
  328.     | HELP CRLF
  329.         {
  330.             help(cmdtab, (char *) 0);
  331.         }
  332.     | HELP SP STRING CRLF
  333.         {
  334.             char *cp = $3;
  335.  
  336.             if (strncasecmp(cp, "SITE", 4) == 0) {
  337.                 cp = $3 + 4;
  338.                 if (*cp == ' ')
  339.                     cp++;
  340.                 if (*cp)
  341.                     help(sitetab, cp);
  342.                 else
  343.                     help(sitetab, (char *) 0);
  344.             } else
  345.                 help(cmdtab, $3);
  346.         }
  347.     | NOOP CRLF
  348.         {
  349.             reply(200, "NOOP command successful.");
  350.         }
  351.     | MKD check_login SP pathname CRLF
  352.         {
  353.             if ($2 && $4 != NULL)
  354.                 makedir($4);
  355.             if ($4 != NULL)
  356.                 free($4);
  357.         }
  358.     | RMD check_login SP pathname CRLF
  359.         {
  360.             if ($2 && $4 != NULL)
  361.                 removedir($4);
  362.             if ($4 != NULL)
  363.                 free($4);
  364.         }
  365.     | PWD check_login CRLF
  366.         {
  367.             if ($2)
  368.                 pwd();
  369.         }
  370.     | CDUP check_login CRLF
  371.         {
  372.             if ($2)
  373.                 cwd("..");
  374.         }
  375.     | SITE SP HELP CRLF
  376.         {
  377.             help(sitetab, (char *) 0);
  378.         }
  379.     | SITE SP HELP SP STRING CRLF
  380.         {
  381.             help(sitetab, $5);
  382.         }
  383.     | SITE SP UMASK check_login CRLF
  384.         {
  385.             int oldmask;
  386.  
  387.             if ($4) {
  388.                 oldmask = umask(0);
  389.                 (void) umask(oldmask);
  390.                 reply(200, "Current UMASK is %03o", oldmask);
  391.             }
  392.         }
  393.     | SITE SP UMASK check_login SP octal_number CRLF
  394.         {
  395.             int oldmask;
  396.  
  397.             if ($4) {
  398.                 if (($6 == -1) || ($6 > 0777)) {
  399.                     reply(501, "Bad UMASK value");
  400.                 } else {
  401.                     oldmask = umask($6);
  402.                     reply(200,
  403.                         "UMASK set to %03o (was %03o)",
  404.                         $6, oldmask);
  405.                 }
  406.             }
  407.         }
  408.     | SITE SP CHMOD check_login SP octal_number SP pathname CRLF
  409.         {
  410.             if ($4 && ($8 != NULL)) {
  411.                 if ($6 > 0777)
  412.                     reply(501,
  413.                 "CHMOD: Mode value must be between 0 and 0777");
  414.                 else if (chmod($8, $6) < 0)
  415.                     perror_reply(550, $8);
  416.                 else
  417.                     reply(200, "CHMOD command successful.");
  418.             }
  419.             if ($8 != NULL)
  420.                 free($8);
  421.         }
  422.     | SITE SP IDLE CRLF
  423.         {
  424.             reply(200,
  425.                 "Current IDLE time limit is %d seconds; max %d",
  426.                 timeout, maxtimeout);
  427.         }
  428.     | SITE SP IDLE SP NUMBER CRLF
  429.         {
  430.             if ($5 < 30 || $5 > maxtimeout) {
  431.                 reply(501,
  432.             "Maximum IDLE time must be between 30 and %d seconds",
  433.                     maxtimeout);
  434.             } else {
  435.                 timeout = $5;
  436.                 (void) alarm((unsigned) timeout);
  437.                 reply(200,
  438.                     "Maximum IDLE time set to %d seconds",
  439.                     timeout);
  440.             }
  441.         }
  442.     | STOU check_login SP pathname CRLF
  443.         {
  444.             if ($2 && $4 != NULL)
  445.                 store($4, "w", 1);
  446.             if ($4 != NULL)
  447.                 free($4);
  448.         }
  449.     | SYST CRLF
  450.         {
  451. #ifdef unix
  452. #ifdef BSD
  453.             reply(215, "UNIX Type: L%d Version: BSD-%d",
  454.                 CHAR_BIT, BSD);
  455. #else /* BSD */
  456. #ifdef linux
  457.             reply(215, "UNIX Type: L%d Version: LINUX", CHAR_BIT);
  458. #else
  459.             reply(215, "UNIX Type: L%d", CHAR_BIT);
  460. #endif /* linux */
  461. #endif /* BSD */
  462. #else /* unix */
  463.             reply(215, "UNKNOWN Type: L%d", CHAR_BIT);
  464. #endif /* unix */
  465.         }
  466.  
  467.         /*
  468.          * SIZE is not in RFC959, but Postel has blessed it and
  469.          * it will be in the updated RFC.
  470.          *
  471.          * Return size of file in a format suitable for
  472.          * using with RESTART (we just count bytes).
  473.          */
  474.     | SIZE check_login SP pathname CRLF
  475.         {
  476.             if ($2 && $4 != NULL)
  477.                 sizecmd($4);
  478.             if ($4 != NULL)
  479.                 free($4);
  480.         }
  481.  
  482.         /*
  483.          * MDTM is not in RFC959, but Postel has blessed it and
  484.          * it will be in the updated RFC.
  485.          *
  486.          * Return modification time of file as an ISO 3307
  487.          * style time. E.g. YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx
  488.          * where xxx is the fractional second (of any precision,
  489.          * not necessarily 3 digits)
  490.          */
  491.     | MDTM check_login SP pathname CRLF
  492.         {
  493.             if ($2 && $4 != NULL) {
  494.                 struct stat stbuf;
  495.                 if (stat($4, &stbuf) < 0)
  496.                     reply(550, "%s: %s",
  497.                         $4, strerror(errno));
  498.                 else if (!S_ISREG(stbuf.st_mode)) {
  499.                     reply(550, "%s: not a plain file.", $4);
  500.                 } else {
  501.                     struct tm *t;
  502.                     t = gmtime(&stbuf.st_mtime);
  503.                     reply(213,
  504.                         "19%02d%02d%02d%02d%02d%02d",
  505.                         t->tm_year, t->tm_mon+1, t->tm_mday,
  506.                         t->tm_hour, t->tm_min, t->tm_sec);
  507.                 }
  508.             }
  509.             if ($4 != NULL)
  510.                 free($4);
  511.         }
  512.     | QUIT CRLF
  513.         {
  514.             reply(221, "Goodbye.");
  515.             dologout(0);
  516.         }
  517.     | error CRLF
  518.         {
  519.             yyerrok;
  520.         }
  521.     ;
  522. rcmd
  523.     : RNFR check_login SP pathname CRLF
  524.         {
  525.             char *renamefrom();
  526.  
  527.             restart_point = (off_t) 0;
  528.             if ($2 && $4) {
  529.                 fromname = renamefrom($4);
  530.                 if (fromname == (char *) 0 && $4) {
  531.                     free($4);
  532.                 }
  533.             }
  534.         }
  535.     | REST SP byte_size CRLF
  536.         {
  537.             fromname = (char *) 0;
  538.             restart_point = $3;    /* XXX $3 is only "int" */
  539.             reply(350, "Restarting at %qd. %s", restart_point,
  540.                 "Send STORE or RETRIEVE to initiate transfer.");
  541.         }
  542.     ;
  543.  
  544. username
  545.     : STRING
  546.     ;
  547.  
  548. password
  549.     : /* empty */
  550.         {
  551.             $$ = (char *)calloc(1, sizeof(char));
  552.         }
  553.     | STRING
  554.     ;
  555.  
  556. byte_size
  557.     : NUMBER
  558.     ;
  559.  
  560. host_port
  561.     : NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA
  562.         NUMBER COMMA NUMBER
  563.         {
  564.             char *a, *p;
  565.  
  566.             /* data_dest.sin_len = sizeof(struct sockaddr_in); */
  567.             data_dest.sin_family = AF_INET;
  568.             p = (char *)&data_dest.sin_port;
  569.             p[0] = $9; p[1] = $11;
  570.             a = (char *)&data_dest.sin_addr;
  571.             a[0] = $1; a[1] = $3; a[2] = $5; a[3] = $7;
  572.         }
  573.     ;
  574.  
  575. form_code
  576.     : N
  577.         {
  578.             $$ = FORM_N;
  579.         }
  580.     | T
  581.         {
  582.             $$ = FORM_T;
  583.         }
  584.     | C
  585.         {
  586.             $$ = FORM_C;
  587.         }
  588.     ;
  589.  
  590. type_code
  591.     : A
  592.         {
  593.             cmd_type = TYPE_A;
  594.             cmd_form = FORM_N;
  595.         }
  596.     | A SP form_code
  597.         {
  598.             cmd_type = TYPE_A;
  599.             cmd_form = $3;
  600.         }
  601.     | E
  602.         {
  603.             cmd_type = TYPE_E;
  604.             cmd_form = FORM_N;
  605.         }
  606.     | E SP form_code
  607.         {
  608.             cmd_type = TYPE_E;
  609.             cmd_form = $3;
  610.         }
  611.     | I
  612.         {
  613.             cmd_type = TYPE_I;
  614.         }
  615.     | L
  616.         {
  617.             cmd_type = TYPE_L;
  618.             cmd_bytesz = CHAR_BIT;
  619.         }
  620.     | L SP byte_size
  621.         {
  622.             cmd_type = TYPE_L;
  623.             cmd_bytesz = $3;
  624.         }
  625.         /* this is for a bug in the BBN ftp */
  626.     | L byte_size
  627.         {
  628.             cmd_type = TYPE_L;
  629.             cmd_bytesz = $2;
  630.         }
  631.     ;
  632.  
  633. struct_code
  634.     : F
  635.         {
  636.             $$ = STRU_F;
  637.         }
  638.     | R
  639.         {
  640.             $$ = STRU_R;
  641.         }
  642.     | P
  643.         {
  644.             $$ = STRU_P;
  645.         }
  646.     ;
  647.  
  648. mode_code
  649.     : S
  650.         {
  651.             $$ = MODE_S;
  652.         }
  653.     | B
  654.         {
  655.             $$ = MODE_B;
  656.         }
  657.     | C
  658.         {
  659.             $$ = MODE_C;
  660.         }
  661.     ;
  662.  
  663. pathname
  664.     : pathstring
  665.         {
  666.             /*
  667.              * Problem: this production is used for all pathname
  668.              * processing, but only gives a 550 error reply.
  669.              * This is a valid reply in some cases but not in others.
  670.              */
  671.             if (logged_in && $1 && *$1 == '~') {
  672.                 glob_t gl;
  673.                 int flags =
  674.                  GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
  675.  
  676.                 memset(&gl, 0, sizeof(gl));
  677.                 if (glob($1, flags, NULL, &gl) ||
  678.                     gl.gl_pathc == 0) {
  679.                     reply(550, "not found");
  680.                     $$ = NULL;
  681.                 } else {
  682.                     $$ = strdup(gl.gl_pathv[0]);
  683.                 }
  684.                 globfree(&gl);
  685.                 free($1);
  686.             } else
  687.                 $$ = $1;
  688.         }
  689.     ;
  690.  
  691. pathstring
  692.     : STRING
  693.     ;
  694.  
  695. octal_number
  696.     : NUMBER
  697.         {
  698.             int ret, dec, multby, digit;
  699.  
  700.             /*
  701.              * Convert a number that was read as decimal number
  702.              * to what it would be if it had been read as octal.
  703.              */
  704.             dec = $1;
  705.             multby = 1;
  706.             ret = 0;
  707.             while (dec) {
  708.                 digit = dec%10;
  709.                 if (digit > 7) {
  710.                     ret = -1;
  711.                     break;
  712.                 }
  713.                 ret += digit * multby;
  714.                 multby *= 8;
  715.                 dec /= 10;
  716.             }
  717.             $$ = ret;
  718.         }
  719.     ;
  720.  
  721.  
  722. check_login
  723.     : /* empty */
  724.         {
  725.             if (logged_in)
  726.                 $$ = 1;
  727.             else {
  728.                 reply(530, "Please login with USER and PASS.");
  729.                 $$ = 0;
  730.             }
  731.         }
  732.     ;
  733.  
  734. %%
  735.  
  736. extern jmp_buf errcatch;
  737.  
  738. #define    CMD    0    /* beginning of command */
  739. #define    ARGS    1    /* expect miscellaneous arguments */
  740. #define    STR1    2    /* expect SP followed by STRING */
  741. #define    STR2    3    /* expect STRING */
  742. #define    OSTR    4    /* optional SP then STRING */
  743. #define    ZSTR1    5    /* SP then optional STRING */
  744. #define    ZSTR2    6    /* optional STRING after SP */
  745. #define    SITECMD    7    /* SITE command */
  746. #define    NSTR    8    /* Number followed by a string */
  747.  
  748. struct tab {
  749.     char    *name;
  750.     short    token;
  751.     short    state;
  752.     short    implemented;    /* 1 if command is implemented */
  753.     char    *help;
  754. };
  755.  
  756. struct tab cmdtab[] = {        /* In order defined in RFC 765 */
  757.     { "USER", USER, STR1, 1,    "<sp> username" },
  758.     { "PASS", PASS, ZSTR1, 1,    "<sp> password" },
  759.     { "ACCT", ACCT, STR1, 0,    "(specify account)" },
  760.     { "SMNT", SMNT, ARGS, 0,    "(structure mount)" },
  761.     { "REIN", REIN, ARGS, 0,    "(reinitialize server state)" },
  762.     { "QUIT", QUIT, ARGS, 1,    "(terminate service)", },
  763.     { "PORT", PORT, ARGS, 1,    "<sp> b0, b1, b2, b3, b4" },
  764.     { "PASV", PASV, ARGS, 1,    "(set server in passive mode)" },
  765.     { "TYPE", TYPE, ARGS, 1,    "<sp> [ A | E | I | L ]" },
  766.     { "STRU", STRU, ARGS, 1,    "(specify file structure)" },
  767.     { "MODE", MODE, ARGS, 1,    "(specify transfer mode)" },
  768.     { "RETR", RETR, STR1, 1,    "<sp> file-name" },
  769.     { "STOR", STOR, STR1, 1,    "<sp> file-name" },
  770.     { "APPE", APPE, STR1, 1,    "<sp> file-name" },
  771.     { "MLFL", MLFL, OSTR, 0,    "(mail file)" },
  772.     { "MAIL", MAIL, OSTR, 0,    "(mail to user)" },
  773.     { "MSND", MSND, OSTR, 0,    "(mail send to terminal)" },
  774.     { "MSOM", MSOM, OSTR, 0,    "(mail send to terminal or mailbox)" },
  775.     { "MSAM", MSAM, OSTR, 0,    "(mail send to terminal and mailbox)" },
  776.     { "MRSQ", MRSQ, OSTR, 0,    "(mail recipient scheme question)" },
  777.     { "MRCP", MRCP, STR1, 0,    "(mail recipient)" },
  778.     { "ALLO", ALLO, ARGS, 1,    "allocate storage (vacuously)" },
  779.     { "REST", REST, ARGS, 1,    "<sp> offset (restart command)" },
  780.     { "RNFR", RNFR, STR1, 1,    "<sp> file-name" },
  781.     { "RNTO", RNTO, STR1, 1,    "<sp> file-name" },
  782.     { "ABOR", ABOR, ARGS, 1,    "(abort operation)" },
  783.     { "DELE", DELE, STR1, 1,    "<sp> file-name" },
  784.     { "CWD",  CWD,  OSTR, 1,    "[ <sp> directory-name ]" },
  785.     { "XCWD", CWD,    OSTR, 1,    "[ <sp> directory-name ]" },
  786.     { "LIST", LIST, OSTR, 1,    "[ <sp> path-name ]" },
  787.     { "NLST", NLST, OSTR, 1,    "[ <sp> path-name ]" },
  788.     { "SITE", SITE, SITECMD, 1,    "site-cmd [ <sp> arguments ]" },
  789.     { "SYST", SYST, ARGS, 1,    "(get type of operating system)" },
  790.     { "STAT", STAT, OSTR, 1,    "[ <sp> path-name ]" },
  791.     { "HELP", HELP, OSTR, 1,    "[ <sp> <string> ]" },
  792.     { "NOOP", NOOP, ARGS, 1,    "" },
  793.     { "MKD",  MKD,  STR1, 1,    "<sp> path-name" },
  794.     { "XMKD", MKD,  STR1, 1,    "<sp> path-name" },
  795.     { "RMD",  RMD,  STR1, 1,    "<sp> path-name" },
  796.     { "XRMD", RMD,  STR1, 1,    "<sp> path-name" },
  797.     { "PWD",  PWD,  ARGS, 1,    "(return current directory)" },
  798.     { "XPWD", PWD,  ARGS, 1,    "(return current directory)" },
  799.     { "CDUP", CDUP, ARGS, 1,    "(change to parent directory)" },
  800.     { "XCUP", CDUP, ARGS, 1,    "(change to parent directory)" },
  801.     { "STOU", STOU, STR1, 1,    "<sp> file-name" },
  802.     { "SIZE", SIZE, OSTR, 1,    "<sp> path-name" },
  803.     { "MDTM", MDTM, OSTR, 1,    "<sp> path-name" },
  804.     { NULL,   0,    0,    0,    0 }
  805. };
  806.  
  807. struct tab sitetab[] = {
  808.     { "UMASK", UMASK, ARGS, 1,    "[ <sp> umask ]" },
  809.     { "IDLE", IDLE, ARGS, 1,    "[ <sp> maximum-idle-time ]" },
  810.     { "CHMOD", CHMOD, NSTR, 1,    "<sp> mode <sp> file-name" },
  811.     { "HELP", HELP, OSTR, 1,    "[ <sp> <string> ]" },
  812.     { NULL,   0,    0,    0,    0 }
  813. };
  814.  
  815. static char    *copy __P((char *));
  816. static void     help __P((struct tab *, char *));
  817. static struct tab *
  818.          lookup __P((struct tab *, char *));
  819. static void     sizecmd __P((char *));
  820. static void     toolong __P((int));
  821. static int     yylex __P((void));
  822.  
  823. static struct tab *
  824. lookup(p, cmd)
  825.     struct tab *p;
  826.     char *cmd;
  827. {
  828.  
  829.     for (; p->name != NULL; p++)
  830.         if (strcmp(cmd, p->name) == 0)
  831.             return (p);
  832.     return (0);
  833. }
  834.  
  835. #include <arpa/telnet.h>
  836.  
  837. /*
  838.  * getline - a hacked up version of fgets to ignore TELNET escape codes.
  839.  */
  840. char *
  841. getline(s, n, iop)
  842.     char *s;
  843.     int n;
  844.     FILE *iop;
  845. {
  846.     int c;
  847.     register char *cs;
  848.  
  849.     cs = s;
  850. /* tmpline may contain saved command from urgent mode interruption */
  851.     for (c = 0; tmpline[c] != '\0' && --n > 0; ++c) {
  852.         *cs++ = tmpline[c];
  853.         if (tmpline[c] == '\n') {
  854.             *cs++ = '\0';
  855.             if (debug)
  856.                 syslog(LOG_DEBUG, "command: %s", s);
  857.             tmpline[0] = '\0';
  858.             return(s);
  859.         }
  860.         if (c == 0)
  861.             tmpline[0] = '\0';
  862.     }
  863.     while ((c = getc(iop)) != EOF) {
  864.         c &= 0377;
  865.         if (c == IAC) {
  866.             if ((c = getc(iop)) != EOF) {
  867.             c &= 0377;
  868.             switch (c) {
  869.             case WILL:
  870.             case WONT:
  871.                 c = getc(iop);
  872.                 printf("%c%c%c", IAC, DONT, 0377&c);
  873.                 (void) fflush(stdout);
  874.                 continue;
  875.             case DO:
  876.             case DONT:
  877.                 c = getc(iop);
  878.                 printf("%c%c%c", IAC, WONT, 0377&c);
  879.                 (void) fflush(stdout);
  880.                 continue;
  881.             case IAC:
  882.                 break;
  883.             default:
  884.                 continue;    /* ignore command */
  885.             }
  886.             }
  887.         }
  888.         *cs++ = c;
  889.         if (--n <= 0 || c == '\n')
  890.             break;
  891.     }
  892.     if (c == EOF && cs == s)
  893.         return (NULL);
  894.     *cs++ = '\0';
  895.     if (debug) {
  896.         if (!guest && strncasecmp("pass ", s, 5) == 0) {
  897.             /* Don't syslog passwords */
  898.             syslog(LOG_DEBUG, "command: %.5s ???", s);
  899.         } else {
  900.             register char *cp;
  901.             register int len;
  902.  
  903.             /* Don't syslog trailing CR-LF */
  904.             len = strlen(s);
  905.             cp = s + len - 1;
  906.             while (cp >= s && (*cp == '\n' || *cp == '\r')) {
  907.                 --cp;
  908.                 --len;
  909.             }
  910.             syslog(LOG_DEBUG, "command: %.*s", len, s);
  911.         }
  912.     }
  913.     return (s);
  914. }
  915.  
  916. static void
  917. toolong(signo)
  918.     int signo;
  919. {
  920.  
  921.     reply(421,
  922.         "Timeout (%d seconds): closing control connection.", timeout);
  923.     if (logging)
  924.         syslog(LOG_INFO, "User %s timed out after %d seconds",
  925.             (pw ? pw -> pw_name : "unknown"), timeout);
  926.     dologout(1);
  927. }
  928.  
  929. static int
  930. yylex()
  931. {
  932.     static int cpos, state;
  933.     char *cp, *cp2;
  934.     struct tab *p;
  935.     int n;
  936.     char c;
  937.  
  938.     for (;;) {
  939.         switch (state) {
  940.  
  941.         case CMD:
  942.             (void) signal(SIGALRM, toolong);
  943.             (void) alarm((unsigned) timeout);
  944.             if (getline(cbuf, sizeof(cbuf)-1, stdin) == NULL) {
  945.                 reply(221, "You could at least say goodbye.");
  946.                 dologout(0);
  947.             }
  948.             (void) alarm(0);
  949. #ifdef HASSETPROCTITLE
  950.             if (strncasecmp(cbuf, "PASS", 4) != NULL)
  951.                 setproctitle("%s: %s", proctitle, cbuf);
  952. #endif /* HASSETPROCTITLE */
  953.             if ((cp = strchr(cbuf, '\r'))) {
  954.                 *cp++ = '\n';
  955.                 *cp = '\0';
  956.             }
  957.             if ((cp = strpbrk(cbuf, " \n")))
  958.                 cpos = cp - cbuf;
  959.             if (cpos == 0)
  960.                 cpos = 4;
  961.             c = cbuf[cpos];
  962.             cbuf[cpos] = '\0';
  963.             upper(cbuf);
  964.             p = lookup(cmdtab, cbuf);
  965.             cbuf[cpos] = c;
  966.             if (p != 0) {
  967.                 if (p->implemented == 0) {
  968.                     nack(p->name);
  969.                     longjmp(errcatch,0);
  970.                     /* NOTREACHED */
  971.                 }
  972.                 state = p->state;
  973.                 yylval.s = p->name;
  974.                 return (p->token);
  975.             }
  976.             break;
  977.  
  978.         case SITECMD:
  979.             if (cbuf[cpos] == ' ') {
  980.                 cpos++;
  981.                 return (SP);
  982.             }
  983.             cp = &cbuf[cpos];
  984.             if ((cp2 = strpbrk(cp, " \n")))
  985.                 cpos = cp2 - cbuf;
  986.             c = cbuf[cpos];
  987.             cbuf[cpos] = '\0';
  988.             upper(cp);
  989.             p = lookup(sitetab, cp);
  990.             cbuf[cpos] = c;
  991.             if (p != 0) {
  992.                 if (p->implemented == 0) {
  993.                     state = CMD;
  994.                     nack(p->name);
  995.                     longjmp(errcatch,0);
  996.                     /* NOTREACHED */
  997.                 }
  998.                 state = p->state;
  999.                 yylval.s = p->name;
  1000.                 return (p->token);
  1001.             }
  1002.             state = CMD;
  1003.             break;
  1004.  
  1005.         case OSTR:
  1006.             if (cbuf[cpos] == '\n') {
  1007.                 state = CMD;
  1008.                 return (CRLF);
  1009.             }
  1010.             /* FALLTHROUGH */
  1011.  
  1012.         case STR1:
  1013.         case ZSTR1:
  1014.         dostr1:
  1015.             if (cbuf[cpos] == ' ') {
  1016.                 cpos++;
  1017.                 state = state == OSTR ? STR2 : ++state;
  1018.                 return (SP);
  1019.             }
  1020.             break;
  1021.  
  1022.         case ZSTR2:
  1023.             if (cbuf[cpos] == '\n') {
  1024.                 state = CMD;
  1025.                 return (CRLF);
  1026.             }
  1027.             /* FALLTHROUGH */
  1028.  
  1029.         case STR2:
  1030.             cp = &cbuf[cpos];
  1031.             n = strlen(cp);
  1032.             cpos += n - 1;
  1033.             /*
  1034.              * Make sure the string is nonempty and \n terminated.
  1035.              */
  1036.             if (n > 1 && cbuf[cpos] == '\n') {
  1037.                 cbuf[cpos] = '\0';
  1038.                 yylval.s = copy(cp);
  1039.                 cbuf[cpos] = '\n';
  1040.                 state = ARGS;
  1041.                 return (STRING);
  1042.             }
  1043.             break;
  1044.  
  1045.         case NSTR:
  1046.             if (cbuf[cpos] == ' ') {
  1047.                 cpos++;
  1048.                 return (SP);
  1049.             }
  1050.             if (isdigit(cbuf[cpos])) {
  1051.                 cp = &cbuf[cpos];
  1052.                 while (isdigit(cbuf[++cpos]))
  1053.                     ;
  1054.                 c = cbuf[cpos];
  1055.                 cbuf[cpos] = '\0';
  1056.                 yylval.i = atoi(cp);
  1057.                 cbuf[cpos] = c;
  1058.                 state = STR1;
  1059.                 return (NUMBER);
  1060.             }
  1061.             state = STR1;
  1062.             goto dostr1;
  1063.  
  1064.         case ARGS:
  1065.             if (isdigit(cbuf[cpos])) {
  1066.                 cp = &cbuf[cpos];
  1067.                 while (isdigit(cbuf[++cpos]))
  1068.                     ;
  1069.                 c = cbuf[cpos];
  1070.                 cbuf[cpos] = '\0';
  1071.                 yylval.i = atoi(cp);
  1072.                 cbuf[cpos] = c;
  1073.                 return (NUMBER);
  1074.             }
  1075.             switch (cbuf[cpos++]) {
  1076.  
  1077.             case '\n':
  1078.                 state = CMD;
  1079.                 return (CRLF);
  1080.  
  1081.             case ' ':
  1082.                 return (SP);
  1083.  
  1084.             case ',':
  1085.                 return (COMMA);
  1086.  
  1087.             case 'A':
  1088.             case 'a':
  1089.                 return (A);
  1090.  
  1091.             case 'B':
  1092.             case 'b':
  1093.                 return (B);
  1094.  
  1095.             case 'C':
  1096.             case 'c':
  1097.                 return (C);
  1098.  
  1099.             case 'E':
  1100.             case 'e':
  1101.                 return (E);
  1102.  
  1103.             case 'F':
  1104.             case 'f':
  1105.                 return (F);
  1106.  
  1107.             case 'I':
  1108.             case 'i':
  1109.                 return (I);
  1110.  
  1111.             case 'L':
  1112.             case 'l':
  1113.                 return (L);
  1114.  
  1115.             case 'N':
  1116.             case 'n':
  1117.                 return (N);
  1118.  
  1119.             case 'P':
  1120.             case 'p':
  1121.                 return (P);
  1122.  
  1123.             case 'R':
  1124.             case 'r':
  1125.                 return (R);
  1126.  
  1127.             case 'S':
  1128.             case 's':
  1129.                 return (S);
  1130.  
  1131.             case 'T':
  1132.             case 't':
  1133.                 return (T);
  1134.  
  1135.             }
  1136.             break;
  1137.  
  1138.         default:
  1139.             fatal("Unknown state in scanner.");
  1140.         }
  1141.         yyerror((char *) 0);
  1142.         state = CMD;
  1143.         longjmp(errcatch,0);
  1144.     }
  1145. }
  1146.  
  1147. void
  1148. upper(s)
  1149.     char *s;
  1150. {
  1151.     while (*s != '\0') {
  1152.         if (islower(*s))
  1153.             *s = toupper(*s);
  1154.         s++;
  1155.     }
  1156. }
  1157.  
  1158. static char *
  1159. copy(s)
  1160.     char *s;
  1161. {
  1162.     char *p;
  1163.  
  1164.     p = malloc((unsigned) strlen(s) + 1);
  1165.     if (p == NULL)
  1166.         fatal("Ran out of memory.");
  1167.     (void) strcpy(p, s);
  1168.     return (p);
  1169. }
  1170.  
  1171. static void
  1172. help(ctab, s)
  1173.     struct tab *ctab;
  1174.     char *s;
  1175. {
  1176.     struct tab *c;
  1177.     int width, NCMDS;
  1178.     char *type;
  1179.  
  1180.     if (ctab == sitetab)
  1181.         type = "SITE ";
  1182.     else
  1183.         type = "";
  1184.     width = 0, NCMDS = 0;
  1185.     for (c = ctab; c->name != NULL; c++) {
  1186.         int len = strlen(c->name);
  1187.  
  1188.         if (len > width)
  1189.             width = len;
  1190.         NCMDS++;
  1191.     }
  1192.     width = (width + 8) &~ 7;
  1193.     if (s == 0) {
  1194.         int i, j, w;
  1195.         int columns, lines;
  1196.  
  1197.         lreply(214, "The following %scommands are recognized %s.",
  1198.             type, "(* =>'s unimplemented)");
  1199.         columns = 76 / width;
  1200.         if (columns == 0)
  1201.             columns = 1;
  1202.         lines = (NCMDS + columns - 1) / columns;
  1203.         for (i = 0; i < lines; i++) {
  1204.             printf("   ");
  1205.             for (j = 0; j < columns; j++) {
  1206.                 c = ctab + j * lines + i;
  1207.                 printf("%s%c", c->name,
  1208.                     c->implemented ? ' ' : '*');
  1209.                 if (c + lines >= &ctab[NCMDS])
  1210.                     break;
  1211.                 w = strlen(c->name) + 1;
  1212.                 while (w < width) {
  1213.                     putchar(' ');
  1214.                     w++;
  1215.                 }
  1216.             }
  1217.             printf("\r\n");
  1218.         }
  1219.         (void) fflush(stdout);
  1220.         reply(214, "Direct comments to ftp-bugs@%s.", hostname);
  1221.         return;
  1222.     }
  1223.     upper(s);
  1224.     c = lookup(ctab, s);
  1225.     if (c == (struct tab *)0) {
  1226.         reply(502, "Unknown command %s.", s);
  1227.         return;
  1228.     }
  1229.     if (c->implemented)
  1230.         reply(214, "Syntax: %s%s %s", type, c->name, c->help);
  1231.     else
  1232.         reply(214, "%s%-*s\t%s; unimplemented.", type, width,
  1233.             c->name, c->help);
  1234. }
  1235.  
  1236. static void
  1237. sizecmd(filename)
  1238.     char *filename;
  1239. {
  1240.     switch (type) {
  1241.     case TYPE_L:
  1242.     case TYPE_I: {
  1243.         struct stat stbuf;
  1244.         if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
  1245.             reply(550, "%s: not a plain file.", filename);
  1246.         else
  1247.             reply(213, "%qu", stbuf.st_size);
  1248.         break; }
  1249.     case TYPE_A: {
  1250.         FILE *fin;
  1251.         int c;
  1252.         off_t count;
  1253.         struct stat stbuf;
  1254.         fin = fopen(filename, "r");
  1255.         if (fin == NULL) {
  1256.             perror_reply(550, filename);
  1257.             return;
  1258.         }
  1259.         if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
  1260.             reply(550, "%s: not a plain file.", filename);
  1261.             (void) fclose(fin);
  1262.             return;
  1263.         }
  1264.  
  1265.         count = 0;
  1266.         while((c=getc(fin)) != EOF) {
  1267.             if (c == '\n')    /* will get expanded to \r\n */
  1268.                 count++;
  1269.             count++;
  1270.         }
  1271.         (void) fclose(fin);
  1272.  
  1273.         reply(213, "%qd", count);
  1274.         break; }
  1275.     default:
  1276.         reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
  1277.     }
  1278. }
  1279.